<?php

namespace app\platform\controller\admin;

use app\BaseController;
use app\common\model\BannerLinkModel;
use app\common\model\BannerModel;


class Banner extends BaseController{

    public function info(){
        $banner = BannerModel::with(['link'=>function($query){

            $query->where(['is_delete'=>0]);

        }])->where(['is_delete'=>0])->select()->toArray();

        return $this->success($banner);
    }
    /*banner单张上传*/
    public function save()
    {
        $banner_id = $this->request->param('banner_id/d', 0);
        $banner_pic_url = $this->request->param('banner_pic_url');
        $type = $this->request->param('type/d', 0);
        $link_type = $this->request->param('link_type/d', 0);
        $link_url_id = $this->request->param('link_url_id/d', 0);
        $content = $this->request->param('content');
        if(!$banner_pic_url) return $this->error('图片地址不能为空');
        if(!$type) return $this->error('banner的点击操作不能为空');
        if($banner_id){
            $banner = BannerModel::where(['banner_id'=>$banner_id])->find();
        }else{
            $banner = new BannerModel();
        }
        $banner->banner_pic_url = $banner_pic_url;
        $banner->type = $type;
        $banner->link_type = $link_type;
        $banner->link_url_id = $link_url_id;
        $banner->content = $content;
        $banner->save();
        return $this->success('设置成功');
    }
    /*banner多张上传*/
    public function saveAll(){
        $data = $this->request->param('data/a',[]);
//        $data[] = [
//            'banner_id'=>1,
//            'banner_pic_url'=>'https://alimg.uumo.top/2d912cfea4e2bdc6966874b082fbb1221683203628.png',
//            'type'=>2,
//            'link_type'=>0,
//            'link_url_id'=>0,
//            'content'=>'测试1',
//        ];
//        $data[] = [
//            'banner_id'=>2,
//            'banner_pic_url'=>'https://alimg.uumo.top/2d912cfea4e2bdc6966874b082fbb1221683203628.png',
//            'type'=>1,
//            'link_type'=>1,
//            'link_url_id'=>1,
//            'content'=>'测试2',
//        ];
//        $data[] = [
//            'banner_pic_url'=>'https://alimg.uumo.top/2d912cfea4e2bdc6966874b082fbb1221683203628.png',
//            'type'=>2,
//            'link_type'=>0,
//            'link_url_id'=>0,
//            'content'=>'测试3',
//        ];
        if($data){
            for($i=0;$i<count($data);$i++){
                if(empty($data[$i]['banner_pic_url'])) return '第'.($i+1).'个banner的图片地址不能为空';
                if(empty($data[$i]['type'])) return '第'.($i+1).'个banner的点击操作不能为空';
            }

            $old_banner = BannerModel::where(['is_effect'=>1])->select()->toArray();

            $update_banner_ids = array_column($data,'banner_id') ?? []; $delete_banner=[];

            $old_banner_ids = array_column($old_banner,'banner_id') ?? [];

            foreach ($old_banner as $k => $v){

                if(!in_array($v['banner_id'],$update_banner_ids)){

                    $v['is_effect']=0;

                    $delete_banner[]=$v;
                }
            }

            foreach ($data as $k => $v){

                if(isset($v['banner_id'])){

                    if(!in_array($v['banner_id'],$old_banner_ids)) unset($data[$k]);

                }

            }

            $new_banner=array_merge($data,$delete_banner);
            //删除

            $banner_model= new BannerModel();

            $banner_model->saveAll($new_banner);


        }
        return $this->success('设置成功');
    }
    public function del(){
        $banner_ids = $this->request->param('banner_ids/a', []);
        if(empty($banner_ids)) return $this->error('参数不能为空');
        BannerModel::whereIn('banner_id',$banner_ids)->save(['is_delete'=>1]);
        return $this->success('删除成功');
    }
    /*改变banner有效状态*/
    public function saveEffect(){
        $banner_id = $this->request->param('banner_id/d', 0);
        $is_effect = $this->request->param('is_effect/d', 0);
        if(!$banner_id) return $this->error('banner不存在');
        if(null === $banner = BannerModel::where(['banner_id'=>$banner_id])->find()) return $this->error('banner不存在');
        $banner->is_effect = $is_effect;
        $banner->save();
        return $this->success('设置成功');
    }

    /*设置链接*/
    public function linkCate(){
        $data[] = ['type'=>1,'name'=>'页面'];
        $data[] = ['type'=>2,'name'=>'产品'];
        $data[] = ['type'=>3,'name'=>'服务'];
        $data[] = ['type'=>4,'name'=>'卡项'];
        $data[] = ['type'=>5,'name'=>'秒杀'];
        $data[] = ['type'=>6,'name'=>'拼团'];
        $data[] = ['type'=>7,'name'=>'券包'];
        return $this->success($data);

    }
    /*设置链接*/
    public function linkList(){
        $type = $this->request->param('type/d',0);
        $link_list = BannerLinkModel::where(['is_delete'=>0,'type'=>$type])->select();
        return $this->success($link_list);
    }


}

